package yuan.consumer;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.TopicPartition;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;

public class ConsumerDemo1 {

    static Properties prop = KafkaConsumerAnalysis.init();
    final static String topic = "topic-demo";


    public static void main(String[] args) {
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(prop);
        TopicPartition topicPartition = new TopicPartition(topic, 0);;
        consumer.assign(Arrays.asList(topicPartition));

        long lastConsumedOffset = -1;

        while (true) {
            final ConsumerRecords<String, String> records = consumer.poll(100);
            if (records.isEmpty()) {
                break;
            }

            final List<ConsumerRecord<String, String>> list = records.records(topicPartition);


            lastConsumedOffset = list.get(list.size() - 1).offset();

            consumer.commitSync();


        }


        System.out.println("lastConsumedOffset:" + lastConsumedOffset);
        final OffsetAndMetadata committed = consumer.committed(topicPartition);
        System.out.println("commited offset:" + committed.offset());
        System.out.println("next position offset: " + consumer.position(topicPartition));


    }


}
